//strip comments from a file.
//
//This is essentially a version of cat(1) that removes standard unix comments,
//that is, any portion of a line starting from #.
//
//EXAMPLE
//
// To keep long lists of arguments to configure: In a file named options:
//      #!/usr/bin/decom
//      --enable-wx5e=3f #This nonsense just means we have a pager
//      --enable-browser
//      --disable-xpcom #this crashes if left enabled
//
// Then we need only run
//      ./configure $(options) 
// or, if you'd rather not chmod the file,
//      ./configure $(decom options)
// rather than remember all those esoteric options. We could do this with cat(1)
// except sh(1) would register the entire file as a comment when parsing it,
// and sometimes people need comments.
package main

import (
	"log"
	"fmt"
	"os"
	"io"
	"bufio"
	"strings"
)

func decom(ri io.Reader) {
	r := bufio.NewReader(ri)

	for {
		line, err := r.ReadString('\n')

		if c := strings.Index(line, "#"); c != -1 {
			line = line[:c] + "\n"
		}

		fmt.Print(line)

		if err != nil {
			if err != os.EOF {
				log.Println(err)
			}

			return
		}
	}
}

//Usage: %name files*
func main() {
	log.SetFlags(0)

	if len(os.Args) == 1 {
		decom(os.Stdin)
		return
	}

	for _, fname := range os.Args[1:] {
		f, err := os.Open(fname)
		if err != nil {
			if fname == "-" {
				decom(os.Stdin)
				continue
			}
			log.Fatalln(err)
		}

		decom(f)
		f.Close()
	}
}
